/*
 * @[H]:  Copyright (c) 2021 Phytium Information Technology, Inc. 
 * 
 *  SPDX-License-Identifier: Apache-2.0. 
 * 
 * @Date: 2021-07-14 09:10:12
 * @LastEditTime: 2021-08-16 15:53:16
 * @Description:  Description of file
 * @Modify History: 
 * * * Ver   Who        Date         Changes
 * * ----- ------     --------    --------------------------------------
 */
#include "f_spi.h"
#include "f_spi_hw.h"

#include "ft_debug.h"
#define FSPI_IRQ_DEBUG_TAG "FSPI_IRQ"
#define FSPI_IRQ_ERROR(format, ...) FT_DEBUG_PRINT_E(FSPI_IRQ_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPI_IRQ_DEBUG_I(format, ...) FT_DEBUG_PRINT_I(FSPI_IRQ_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPI_IRQ_DEBUG_W(format, ...) FT_DEBUG_PRINT_W(FSPI_IRQ_DEBUG_TAG, format, ##__VA_ARGS__)

extern void FSpiIrqTransfer(FSpi *instance_p, u16 irq_status);

void FSpiInterruptHandler(s32 vector, FSpi *instance_p)
{
    FT_ASSERTVOID(instance_p != NULL);
    FT_ASSERTVOID(instance_p->is_ready == FT_COMPONENT_IS_READY);
    FSpiConfig *config_p;
    u16 irq_status;
    (void)vector;
    config_p = &instance_p->config;
    irq_status = FSPI_READREG32(config_p->base_address, FSPI_ISR_OFFSET) & 0x3f;

    if (!irq_status)
    {
        FSPI_IRQ_DEBUG_W("No isr trriger ");
        return;
    }

    FSpiIrqTransfer(instance_p, irq_status);
}

void FSpiIrqCallbackRegister(FSpi *instance_p, u32 select_index, FSpiIrqCallBack callback_fun, void *arg)
{
    FT_ASSERTVOID(instance_p != NULL);
    FT_ASSERTVOID(instance_p->is_ready == FT_COMPONENT_IS_READY);

    switch (select_index)
    {
    case FSPI_IRQ_FULL_CALLBACK_REGISTER:
        instance_p->fspi_full_callback = callback_fun;
        instance_p->full_args = arg;

        break;
    case FSPI_IRQ_FIN_CALLBACK_REGISTER:
        instance_p->fspi_recv_fin_callback = callback_fun;
        instance_p->recv_args = arg;
        break;
    default:
        FSPI_IRQ_DEBUG_W("warning select index %ld ", select_index);
        return;
    }
}